
const _df ={
	 ...getApp().globalData.util,
	rewardAdContent:null,
	init(){
		
	},
	reward_init(){
		this.rewardAdContent=null;
		let rewardInfo=this.getRewardInfo();
		if(!!rewardInfo){
			// #ifdef MP-WEIXIN
						if (wx.createRewardedVideoAd ) {
						  this.rewardAdContent = wx.createRewardedVideoAd({
							adUnitId: rewardInfo.unit_id
						  })
						}
			// #endif
			// #ifdef MP-KUAISHOU
						this.rewardAdContent = ks.createRewardedVideoAd({
				            type: rewardInfo.unit_type,
				            unitId: rewardInfo.unit_id,
				        });
			// #endif
			// #ifdef MP-TOUTIAO
						console.log('reward_init douying',rewardInfo);
						this.rewardAdContent =tt.createRewardedVideoAd({
				            adUnitId: rewardInfo.unit_id
				        });
			// #endif
		}
	},
	showReward(completed,cancel){
		console.log('showReward 1');
		if(!this.rewardAdContent) this.reward_init();
		console.log('showReward 2');
		// #ifdef MP-WEIXIN
				this.rewardAdContent.onLoad(() => { console.log('rewarAd Loaded Sucess!')})
				this.rewardAdContent.onError((err) => { 
					console.log('rewardAd Load Err',err,JSON.stringify(err))
					this.clear()
					});
				 this.rewardAdContent.onClose((res) => {
					res.isEnded?(completed && completed()):(cancel && cancel())
					this.clear()
				})
				this.rewardAdContent.show().catch(() => {
				    // 失败重试
				    this.rewardAdContent.load()
				      .then(() => this.rewardAdContent.show())
				      .catch(err => {
				        console.log('激励视频 广告显示失败')
						this.clear()
				      })
				  })
		// #endif
		
		// #ifdef MP-KUAISHOU
				this.rewardAdContent.onClose(({isEnded}) => {
					console.log('showReward 4-------------------------------------- close isEnd:',isEnded);
		           isEnded?(completed && completed()):(cancel && cancel())
				   this.clear()
		        });
				
				console.log('showReward 333');
				this.rewardAdContent.onLoad(() => {
				    console.log('onLoad event emit');
				});
				this.rewardAdContent.onError(({ errCode }) => {
				    console.log('onError event emit', errCode);
					this.clear()
				});
				
				this.rewardAdContent.show()
				            .catch(() => {
				                this.rewardAdContent.load()
				                    .then(() => this.rewardAdContent.show())
				                    .catch(err => {
				                        console.log('激励视频广告显示失败');
										this.clear()
				                    });
				            });
		// #endif
		// #ifdef MP-TOUTIAO
				this.rewardAdContent.onLoad(() => { console.log('rewarAd Loaded Sucess!')})
				this.rewardAdContent.onError((err) => {
					console.log('rewardAd Load Err',err,JSON.stringify(err))
					this.clear()
					});
				 this.rewardAdContent.onClose((res) => {
					res.isEnded?(completed && completed()):(cancel && cancel())
					this.clear()
				})
				this.rewardAdContent.show().catch(() => {
				    // 失败重试
					console.log('rewardAd show')
					  this.rewardAdContent.load()
				      .then(() => this.rewardAdContent.show())
				      .catch(err => {
				        console.log('激励视频 广告显示失败')
						this.clear()
				      })
				  })
		// #endif
		

	},
	clear(){
		if(!!this.rewardAdContent){
			console.log('rewardAd clear')
			// #ifdef MP-WEIXIN
				this.rewardAdContent.offClose()
				this.rewardAdContent.offError()
				this.rewardAdContent.offLoad()
			// #endif
			
			// #ifdef MP-KUAISHOU
				this.rewardAdContent.destroy();
			// #endif
			// #ifdef MP-MP-TOUTIAO
				this.rewardAdContent.offClose()
				this.rewardAdContent.offError()
				this.rewardAdContent.offLoad()
			// #endif
		}
		this.rewardAdContent=null;
	}
};

export default _df;